﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Ngram
{
    public class PProcess
    {
        private List<string> punctuation = new List<string>();
        private List<string> sentencesUS = new List<string>();
        private List<string> sentencesS = new List<string>();

        private Model1 m1;
        private Model2 m2;
        private Model3 m3;
        private Model4 m4;

        public enum NgramModel
        {
            TypeI,TypeII,TypeIII,TypeIV
        }

        private static string PATTERN_PUNCTUATION = "[\n.,;“”:()!?'`\\\"/-] ?";

        public PProcess()
        {
            this.m1 = new Model1();
            this.m2 = new Model2();
            this.m3 = new Model3();
            this.m4 = new Model4();
        }

        private void splitSentence(string str)
        {
            MatchCollection mc = Regex.Matches(str, PATTERN_PUNCTUATION);
            foreach (Match m in mc)
            {
                punctuation.Add(m.Value);
            }
            foreach (string str1 in Regex.Split(str, PATTERN_PUNCTUATION))
            {
                sentencesUS.Add(str1);
            }
        }

        public string process(string str,NgramModel nm)
        {
            str = Utils.processUtils.ConvertToUnSign(str.ToLower());
            string kq = "";
            splitSentence(str);
            foreach (string s in sentencesUS)
            {
                switch (nm)
                {
                    case NgramModel.TypeI:
                        sentencesS.Add(m1.process(s));
                        break;
                    case NgramModel.TypeII:
                        sentencesS.Add(m2.process(s));
                        break;
                    case NgramModel.TypeIII:
                        sentencesS.Add(m3.process(s));
                        break;
                    case NgramModel.TypeIV:
                        sentencesS.Add(m4.process(s));
                        break;
                    default:
                        break;
                }
            }
            if (sentencesS.Count > punctuation.Count)
            {
                for (int i = 0; i < punctuation.Count; i++)
                {
                    kq += sentencesS[i] + punctuation[i];
                }
                kq += sentencesS[sentencesS.Count - 1];
            }
            if (sentencesS.Count < punctuation.Count)
            {
                for (int i = 0; i < sentencesS.Count; i++)
                {
                    kq += sentencesS[i] + punctuation[i];
                }
                kq += punctuation[punctuation.Count - 1];
            }
            if (sentencesS.Count == punctuation.Count)
            {
                for (int i = 0; i < sentencesS.Count; i++)
                {
                    kq += sentencesS[i] + punctuation[i];
                }
            }
            sentencesUS.Clear();
            punctuation.Clear();
            sentencesS.Clear();
            return kq;
        }
    }
}
